package com.aier.admin.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.eicp.ghostboy.futil.RandomUtil;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.aier.common.common.Constant;

public class AdminExcelUtil {

    /**
     * 默认类型 0卡头 AA 1卡身 0000 2卡尾 1234 3十六进制ic内卡号
     */
    public static final int TYPE_DEFAULT = 0;

    public static HSSFWorkbook createExcel(String[] sign, int sn, int en,
            int sleng) {
        HSSFWorkbook hwb = new HSSFWorkbook();
        for (int i = 0; i < sign.length; i++) {
            addShell(hwb, sign[i], sn, en, sleng);
        }
        return hwb;
    }

    private static void addShell(HSSFWorkbook hwb, String sign, int sn, int en,
            int sleng) {
        HSSFSheet sheet = hwb.createSheet(sign);
        for (int i = 0; i <= (en - sn); i++) {
            HSSFRow row = sheet.createRow(i);
            row.createCell(0).setCellValue(
                    sign + AdminUtil.leftPading(String.valueOf(i + sn), "0", sleng));
            row.createCell(1).setCellValue(
                    RandomUtil.genRandomNum(Constant.num_4, RandomUtil.PWD_TYPE_NUM));
        }
    }

    public static List<String[]> parseExcel(File f, String fileContentType,
            int type) {

        Workbook wb = null;
        try {
            if ("application/vnd.ms-excel".equals(fileContentType)) {
                wb = new HSSFWorkbook(new FileInputStream(f));
            } else if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                    .equals(fileContentType)) {
                try {
                    wb = new XSSFWorkbook(new FileInputStream(f));
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }else{
            	wb = new HSSFWorkbook(new FileInputStream(f));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        switch (type) {
            case TYPE_DEFAULT:
                try {
                    return parseExcelDefault(wb);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            default:
                return null;
        }
    }

    private static List<String[]> parseExcelDefault(Workbook wb) {
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            parseExcelDefaultSheel(wb.getSheetAt(0), list);
        }
        return list;
    }

    private static void parseExcelDefaultSheel(Sheet sheet, List<String[]> list) {

        for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
            Row row = sheet.getRow(j);
            if (row != null){
                String ic = changeString(row.getCell(0));
                list.add(new String[] { ic.substring(0, 2),
                        ic.substring(2, ic.length()),
                        AdminUtil.leftPading(changeString(row.getCell(1)), "0", Constant.num_4),
                        changeString(row.getCell(2)) });                
            }

        }
    }

    private static String changeString(Cell cell) {
        if (null == cell) {
            return null;
        }
        String tmp = null;
        try {
            tmp = cell.getStringCellValue();
        } catch (IllegalStateException e) {
            tmp = String.valueOf((long)cell.getNumericCellValue());
        }
        return tmp;
    }

}
